package com.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.entity.Article;
import com.entity.Cart;
import com.entity.Cate;
import com.entity.Complains;
import com.entity.Details;
import com.entity.Fav;
import com.entity.Foods;
import com.entity.Hist;
import com.entity.Orders;
import com.entity.Topic;
import com.entity.Users;
import com.service.CateService;
import com.service.CosService;
import com.service.DetailsService;
import com.service.FavService;
import com.service.FoodsService;
import com.service.UsersService;
import com.util.PageHelper;
import com.util.VeDate;

//源码作者QQ 1305637939    6524
//定义为控制器
@Controller
// 设置路径
@RequestMapping("/index")
public class IndexController extends BaseController {

	@Autowired
	private UsersService usersService;
	@Autowired
	private CateService cateService;
	@Autowired
	private FoodsService foodsService;
	@Autowired
	private DetailsService detailsService;
	@Autowired
	private FavService favService;
	@Autowired
	private CosService cosService;

	// 内容(余弦函数)算法调用
	private void cos(String cateid) {
		// 推荐食品的算法 1 用户未登录 直接调用foodsService的getGoodsByHot方法
		// 2 用户登录 但是没有浏览食品 则调用1
		// 3 用户登录 且有浏览的食品 调用cosService 通过协同过滤算法 自动推荐食品
		if (this.getSession().getAttribute("userid") == null) {
			List<Foods> buyList = this.foodsService.getFoodsByRandom(cateid);
			this.getRequest().setAttribute("buyList", buyList);
		} else {
			String userid = (String) this.getSession().getAttribute("userid");
			List<Foods> buyList = this.cosService.getRecommend(userid, cateid);
			this.getRequest().setAttribute("buyList", buyList);
		}
	}

	// 协同过滤算法调用
	private void recommend() {
		// 推荐食品的算法 1 用户未登录 直接调用foodsService的getGoodsByHot方法
		// 2 用户登录 但是没有浏览食品 则调用1
		// 3 用户登录 且有浏览的食品 调用recommendService 通过协同过滤算法 自动推荐食品
		if (this.getSession().getAttribute("userid") == null) {
			List<Foods> hotList = this.foodsService.getFoodsByHot();
			this.getRequest().setAttribute("hotList", hotList);
		}
	}

	// 公共方法 提供公共查询数据
	private void front() {
		this.getRequest().setAttribute("title", "美食推荐系统");
		List<Cate> cateList = this.cateService.getAllCate();
		this.getRequest().setAttribute("cateList", cateList);
	}

	// 首页显示
	@RequestMapping("index.action")
	public String index() {
		this.front();
		List<Cate> cateList = this.cateService.getCateFront();
		List<Cate> frontList = new ArrayList<Cate>();
		for (Cate cate : cateList) {
			List<Foods> foodsList = this.foodsService.getFoodsByCate(cate.getCateid());
			cate.setFoodsList(foodsList);
			frontList.add(cate);
		}
		this.getRequest().setAttribute("frontList", frontList);
		return "users/index";
	}

	// 公告
	@RequestMapping("article.action")
	public String article(String number) {
		this.front();
		return "users/article";
	}

	// 阅读公告
	@RequestMapping("read.action")
	public String read(String id) {
		this.front();
		return "users/read";
	}

	// 准备登录
	@RequestMapping("preLogin.action")
	public String prelogin() {
		this.front();
		return "users/login";
	}

	// 用户登录
	@RequestMapping("login.action")
	public String login() {
		this.front();
		String username = this.getRequest().getParameter("username");
		String password = this.getRequest().getParameter("password");
		Users u = new Users();
		u.setUsername(username);
		List<Users> usersList = this.usersService.getUsersByCond(u);
		if (usersList.size() == 0) {
			this.getSession().setAttribute("message", "用户名不存在");
			return "redirect:/index/preLogin.action";
		} else {
			Users users = usersList.get(0);
			if (password.equals(users.getPassword())) {
				this.getSession().setAttribute("userid", users.getUsersid());
				this.getSession().setAttribute("username", users.getUsername());
				this.getSession().setAttribute("users", users);
				return "redirect:/index/index.action";
			} else {
				this.getSession().setAttribute("message", "密码错误");
				return "redirect:/index/preLogin.action";
			}
		}
	}

	// 准备注册
	@RequestMapping("preReg.action")
	public String preReg() {
		this.front();
		return "users/register";
	}

	// 用户注册
	@RequestMapping("register.action")
	public String register(Users users) {
		this.front();
		Users u = new Users();
		u.setUsername(users.getUsername());
		List<Users> usersList = this.usersService.getUsersByCond(u);
		if (usersList.size() == 0) {
			users.setRegdate(VeDate.getStringDateShort());
			this.usersService.insertUsers(users);
		} else {
			this.getSession().setAttribute("message", "用户名已存在");
			return "redirect:/index/preReg.action";
		}

		return "redirect:/index/preLogin.action";
	}

	// 退出登录
	@RequestMapping("exit.action")
	public String exit() {
		this.front();
		this.getSession().removeAttribute("userid");
		this.getSession().removeAttribute("username");
		this.getSession().removeAttribute("users");
		return "redirect:/index/index.action";
	}

	// 准备修改密码
	@RequestMapping("prePwd.action")
	public String prePwd() {
		this.front();
		if (this.getSession().getAttribute("userid") == null) {
			return "redirect:/index/preLogin.action";
		}
		return "users/editpwd";
	}

	// 修改密码
	@RequestMapping("editpwd.action")
	public String editpwd() {
		this.front();
		if (this.getSession().getAttribute("userid") == null) {
			return "redirect:/index/preLogin.action";
		}
		String userid = (String) this.getSession().getAttribute("userid");
		String password = this.getRequest().getParameter("password");
		String repassword = this.getRequest().getParameter("repassword");
		Users users = this.usersService.getUsersById(userid);
		if (password.equals(users.getPassword())) {
			users.setPassword(repassword);
			this.usersService.updateUsers(users);
		} else {
			this.getSession().setAttribute("message", "旧密码错误");
			return "redirect:/index/prePwd.action";
		}
		this.getSession().setAttribute("message", "修改成功");
		return "redirect:/index/prePwd.action";
	}

	@RequestMapping("usercenter.action")
	public String usercenter() {
		this.front();
		if (this.getSession().getAttribute("userid") == null) {
			return "redirect:/index/preLogin.action";
		}
		return "users/usercenter";
	}

	@RequestMapping("userinfo.action")
	public String userinfo() {
		this.front();
		if (this.getSession().getAttribute("userid") == null) {
			return "redirect:/index/preLogin.action";
		}
		String userid = (String) this.getSession().getAttribute("userid");
		this.getSession().setAttribute("users", this.usersService.getUsersById(userid));
		return "users/userinfo";
	}

	@RequestMapping("personal.action")
	public String personal(Users users) {
		this.front();
		if (this.getSession().getAttribute("userid") == null) {
			return "redirect:/index/preLogin.action";
		}
		this.usersService.updateUsers(users);
		this.getSession().setAttribute("message", "修改成功");
		return "redirect:/index/userinfo.action";
	}

	// 添加食品到购物车
	@RequestMapping("addcart.action")
	public String addcart() {
		this.front();
		return "redirect:/index/cart.action";
	}

	// 查看购物车
	@RequestMapping("cart.action")
	public String cart() {
		this.front();
		return "users/cart";
	}

	// 删除购物车中的食品
	@RequestMapping("deletecart.action")
	public String deletecart(String id) {
		this.front();
		return "redirect:/index/cart.action";
	}

	// 准备结算
	@RequestMapping("preCheckout.action")
	public String preCheckout() {
		this.front();
		return "users/checkout";
	}

	// 结算
	@RequestMapping("checkout.action")
	public String checkout() {
		this.front();
		return "redirect:/index/showOrders.action";
	}

	// 查看订购
	@RequestMapping("showOrders.action")
	public String showOrders(String number) {
		this.front();
		return "users/orderlist";
	}

	// 准备付款
	@RequestMapping("prePay.action")
	public String prePay(String id) {
		this.front();
		return "users/pay";
	}

	// 付款
	@RequestMapping("pay.action")
	public String pay(String id) {
		this.front();
		return "redirect:/index/showOrders.action";
	}

	// 确认完成
	@RequestMapping("over.action")
	public String over(String id) {
		this.front();
		return "redirect:/index/showOrders.action";
	}

	// 取消订单
	@RequestMapping("cancel.action")
	public String cancel(String id) {
		this.front();
		return "redirect:/index/showOrders.action";
	}

	// 订单明细
	@RequestMapping("orderdetail.action")
	public String orderdetail(String id) {
		this.front();
		if (this.getSession().getAttribute("userid") == null) {
			return "redirect:/index/preLogin.action";
		}
		Details details = new Details();
		details.setOrdercode(id);
		List<Details> detailsList = this.detailsService.getDetailsByCond(details);
		this.getRequest().setAttribute("detailsList", detailsList);
		return "users/orderdetail";
	}

	// 按分类查询
	@RequestMapping("cate.action")
	public String cate(String id, String number) {
		this.front();
		Foods foods = new Foods();
		foods.setCateid(id);
		List<Foods> tempList = this.foodsService.getFoodsByCond(foods);
		PageHelper.getIndexPage(tempList, "foods", "cate", id, 12, number, this.getRequest());
		this.recommend();
		return "users/list";
	}

	// 推荐食品
	@RequestMapping("recommend.action")
	public String recommend(String number) {
		this.front();
		Foods foods = new Foods();
		foods.setRecommend("是");
		List<Foods> tempList = this.foodsService.getFoodsByCond(foods);
		PageHelper.getIndexPage(tempList, "foods", "recommend", null, 12, number, this.getRequest());
		this.recommend();
		return "users/list";
	}

	// 特价食品
	@RequestMapping("special.action")
	public String special(String number) {
		this.front();
		return "users/list";
	}

	// 全部食品
	@RequestMapping("all.action")
	public String all(String number) {
		this.front();
		return "users/list";
	}

	// 查询食品
	@RequestMapping("query.action")
	public String query(String name) {
		this.front();
		Foods foods = new Foods();
		foods.setFoodsname(name);
		List<Foods> foodsList = this.foodsService.getFoodsByLike(foods);
		this.getRequest().setAttribute("foodsList", foodsList);
		this.recommend();
		return "users/list";
	}

	// 食品详情
	@RequestMapping("detail.action")
	public String detail(String id) {
		this.front();
		return "users/detail";
	}

	@RequestMapping("preTopic.action")
	public String preTopic(String id) {
		this.front();
		return "users/addTopic";
	}

	@RequestMapping("addTopic.action")
	public String addTopic(Topic topic) {
		this.front();
		return "redirect:/index/showOrders.action";
	}

	// 添加收藏
	@RequestMapping("addfav.action")
	public String addfav(Fav fav) {
		this.front();
		if (this.getSession().getAttribute("userid") == null) {
			return "redirect:/index/preLogin.action";
		}
		String userid = (String) getRequest().getSession().getAttribute("userid");
		fav.setAddtime(VeDate.getStringDateShort());
		fav.setFoodsid(getRequest().getParameter("id"));
		fav.setUsersid(userid);
		this.favService.insertFav(fav);
		return "redirect:/index/myfav.action";
	}

	// 我的收藏
	@RequestMapping("myfav.action")
	public String myfav(String number) {
		this.front();
		if (getRequest().getSession().getAttribute("userid") == null) {
			return "redirect:/index/preLogin.action";
		}
		String userid = (String) this.getSession().getAttribute("userid");
		Fav fav = new Fav();
		fav.setUsersid(userid);
		List<Fav> favList = this.favService.getFavByCond(fav);
		PageHelper.getIndexPage(favList, "fav", "myfav", null, 10, number, this.getRequest());
		return "users/myfav";
	}

	// 删除收藏
	@RequestMapping("deletefav.action")
	public String deletefav() {
		this.front();
		if (this.getSession().getAttribute("userid") == null) {
			return "redirect:/index/preLogin.action";
		}
		this.favService.deleteFav(this.getRequest().getParameter("id"));
		return "redirect:/index/myfav.action";
	}

	@RequestMapping("preComplains.action")
	public String preComplains() {
		this.front();
		if (this.getSession().getAttribute("userid") == null) {
			return "redirect:/index/preLogin.action";
		}
		return "users/addComplains";
	}

	@RequestMapping("addComplains.action")
	public String addComplains(Complains complains) {
		this.front();
		return "redirect:/index/preComplains.action";
	}

	@RequestMapping("myComplains.action")
	public String myComplains(String number) {
		this.front();
		return "users/myComplains";
	}
}
